package com.iteheima.subwaySystem;

import com.iteheima.domain.Manager;
import com.iteheima.domain.Subway;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException {
        ArrayList<Manager> managerList = readManager();
        ArrayList<Subway> subwayList = readSubway();
        Scanner scanner = new Scanner(System.in);

        if (isIdentityCheck(managerList)) {
            System.out.println("登录成功");
//            while (true) {//@测试
                System.out.println("1.查看地铁站 2.添加地铁站");
//                System.out.println("3.退出程序");//@测试
                int choice = scanner.nextInt();
                while (choice > 2 || choice < 1) {
//                while (choice > 3 || choice < 1) {//@测试
                    System.out.println("输入有误，请重新选择：");
                    System.out.println("1.查看地铁站 2.添加地铁站");//@测试
                    choice = scanner.nextInt();
                }
                if (choice == 1) {
                    checkSubways(subwayList);
                } else if (choice == 2) {
                    addSubways(subwayList);
                }
                /*else if (choice == 3) {//@测试
                    System.exit(1);//@测试
                }//@测试
            }//@测试*/
        }
        else {
            System.out.println("登录失败。程序结束");
            System.exit(1);
        }
    }

    private static void addSubways(ArrayList<Subway> subwayList) throws IOException {
        Scanner scanner = new Scanner(System.in);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Test0922\\subways.txt"));

        System.out.println("请依次输入如下信息：");
        System.out.println("地铁站编号");
        String subwayId = scanner.nextLine();
        System.out.println("地铁站名称");
        String subwayName = scanner.nextLine();
        System.out.println("所属路线");
        String subwayLine = scanner.nextLine();

        Subway subway = new Subway(subwayId, subwayName, subwayLine);
        subwayList.add(subway);

        for (Subway sbw : subwayList) {
            String s = sbw.getSubwayId() + "," + sbw.getSubwayName() + "," + sbw.getSubwayLine() + "\r\n";
            byte[] bytes = s.getBytes();
            bos.write(bytes);
        }
        bos.close();
        System.out.println("添加地铁站信息成功");
    }

    private static void checkSubways(ArrayList<Subway> subwayList) {
        if (subwayList == null){
            System.out.println("暂无地铁站信息，欢迎添加");
        }else {
            for (Subway subway : subwayList) {
                System.out.println(subway);
            }
        }
    }

    private static boolean isIdentityCheck(ArrayList<Manager> managerList) {
        System.out.println("请进行身份验证");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入工号");
        String managerId = scanner.nextLine();
        System.out.println("请输入姓名");
        String managerName = scanner.nextLine();
        boolean identityCheck = false;
        for (Manager manager : managerList) {
            if (managerId.equals(manager.getManagerId())){
                if (managerName.equals(manager.getManagerName())){
                    identityCheck = true;
                }
            }
        }
        return identityCheck;
    }

    private static ArrayList<Subway> readSubway() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Test0922\\subways.txt"));
        StringBuilder stringBuilder = new StringBuilder();
        int len;
        byte[] bytes = new byte[1024];

        String id;
        String name;
        String line;

        ArrayList<Subway> subwayList = new ArrayList<>();
        while ((len = bis.read(bytes)) != -1){
            String s = new String(bytes, 0, len);
            stringBuilder.append(s);
        }
        bis.close();

        String target = stringBuilder.toString();

        if ("".equals(target.trim())){
            return null;
        }

        String[] block = target.split("\r\n");
        for (int i = 0; i < block.length; i++) {
            if (!"".equals(block[i])) {
                String[] attribute = block[i].split(",");
                id = attribute[0];
                name = attribute[1];
                line = attribute[2];
                Subway subway = new Subway(id, name, line);
                subwayList.add(subway);
            }
        }
        return subwayList;
    }

    private static ArrayList<Manager> readManager() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Test0922\\managers.txt"));
        StringBuilder stringBuilder = new StringBuilder();
        int len;
        byte[] bytes = new byte[1024];
        String id;
        String name;
        String gender;
        String phone;
        ArrayList<Manager> managerList = new ArrayList<>();

        while ((len = bis.read(bytes)) != -1){
            String s = new String(bytes, 0, len);
            stringBuilder.append(s);
        }
        bis.close();

        String target = stringBuilder.toString();
        if ("".equals(target.trim())){
            return null;
        }

        String[] block = target.split("\r\n");
        for (int i = 0; i < block.length; i++) {
            if (!"".equals(block[i].trim())){
                String[] attribute = block[i].split(",");
                id = attribute[0];
                name = attribute[1];
                gender = attribute[2];
                phone = attribute[3];
                Manager manager = new Manager(id, name, gender, phone);
                managerList.add(manager);
            }
        }
        return managerList;
    }

}
